package com.blue.controller.system;

import com.blue.common.annotation.ReplaceParam;
import com.blue.common.constant.CommonConstant;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.github.yulichang.query.MPJQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blue.common.utils.QueryWrapperUtils;
import org.springframework.web.bind.annotation.*;
import com.blue.common.utils.ConvertUtils;
import com.blue.po.system.LoginInfoPO;
import com.blue.dto.system.LoginInfoDTO;
import com.blue.service.system.ILoginInfoService;
import com.blue.common.entity.ResponseResult;

import java.util.List;


/**
 * 登录信息表
 *
 * @author nanjie.chen
 * @email 15696471158@163.com
 * @date 2025-01-19
 */
@RestController
@RequestMapping("/system/loginInfo")
@Tag(name = "登录信息表")
public class LoginInfoController {
    @Autowired
    private ILoginInfoService loginInfoService;

    @GetMapping("/pageList")
    @Operation(summary = "列表查询")
    @ReplaceParam(value = "loginAccount", position = "right")
    public ResponseResult<?> list(LoginInfoDTO requestDTO,
                                  @RequestParam(value = "pageSize", defaultValue = "10") long pageSize,
                                  @RequestParam(value = "pageNum", defaultValue = "1") long pageNum) {
        String startTime = requestDTO.getStartTime();
        String endTime = requestDTO.getEndTime();
        LoginInfoPO loginInfoPO = ConvertUtils.convert(LoginInfoPO.class, requestDTO);
        MPJQueryWrapper<LoginInfoPO> queryWrapper = QueryWrapperUtils.initQueryWrapper(loginInfoPO, "t.login_time", CommonConstant.DESC);
        queryWrapper.ge(StringUtils.isNotBlank(startTime), "t.login_time", startTime);
        queryWrapper.le(StringUtils.isNotBlank(endTime), "t.login_time", endTime);
        Page<LoginInfoPO> page = new Page<>(pageNum, pageSize);
        Page<LoginInfoPO> pageResult = loginInfoService.page(page, queryWrapper);
        return ResponseResult.okResult(pageResult);
    }

    @PostMapping("/add")
    @Operation(summary = "新增")
    public ResponseResult<?> add(@RequestBody LoginInfoDTO requestDTO) {
        LoginInfoPO loginInfoPO = ConvertUtils.convert(LoginInfoPO.class, requestDTO);
        loginInfoService.save(loginInfoPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/edit")
    @Operation(summary = "修改")
    public ResponseResult<?> edit(@RequestBody LoginInfoDTO requestDTO) {
        LoginInfoPO loginInfoPO = ConvertUtils.convert(LoginInfoPO.class, requestDTO);
        loginInfoService.updateById(loginInfoPO);
        return ResponseResult.okResult();
    }

    @PostMapping("/delete")
    @Operation(summary = "删除")
    public ResponseResult<?> delete(@RequestBody List<String> ids) {
        loginInfoService.removeBatchByIds(ids);
        return ResponseResult.okResult();
    }

    @PostMapping("/queryById")
    @Operation(summary = "通过ID查询")
    public ResponseResult<?> queryById(@RequestBody LoginInfoDTO requestDTO) {
        LoginInfoPO loginInfoPO = loginInfoService.getById(requestDTO.getId());
        return ResponseResult.okResult(loginInfoPO);
    }
}
